package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {


    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrdersMapper ordersMapper;


    @Override
    @Transactional
    public void submit(Orders orders) {
        // TODO 1. 基础数据的准备
        //用户 User
        Long userId = BaseContext.getCurrentId();
        User user = userService.getById(userId);
        //购物车 List<ShoppingCart>
        List<ShoppingCart> shoppingCartList = shoppingCartService.query().eq("user_id", userId).list();
        //地址 AddressBook
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        //生成一个订单id，采用雪花算法生产
        long orderId = IdWorker.getId();
        //定义一个变量，总金额
        BigDecimal total = new BigDecimal(0);

        //TODO 2. 添加N条订单项 order_detail
        //定义一个集合
        List<OrderDetail> list = new ArrayList<>();
        for (ShoppingCart cart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            //补全数据
            orderDetail.setOrderId(orderId);//订单id
            orderDetail.setNumber(cart.getNumber());//设置数量
            orderDetail.setDishFlavor(cart.getDishFlavor());//口味
            orderDetail.setDishId(cart.getDishId());//菜品id
            orderDetail.setSetmealId(cart.getSetmealId());//套餐id
            orderDetail.setName(cart.getName());//名称
            orderDetail.setImage(cart.getImage());//图片
            orderDetail.setAmount(cart.getAmount());//金额 单价
            //计算总金额
            BigDecimal multiply = cart.getAmount().multiply(new BigDecimal(cart.getNumber()));
            total = total.add(multiply);
            //将orderDetail放入集合中
            list.add(orderDetail);
        }

        //TODO 3. 添加订单数据 orders
        //补全信息
        orders.setOrderTime(LocalDateTime.now());//下单时间
        orders.setCheckoutTime(LocalDateTime.now());//支付时间
        orders.setStatus(4);//状态 订单状态 1待付款，2待派送，3已派送，4已完成，5已取消
        orders.setUserId(userId);//用户id
        orders.setId(orderId);
        orders.setNumber(orderId + "");//订单编号
        orders.setAmount(total);// 总金额
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());//收获人
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());


        //TODO 4. 保存订单，订单项集合，顺便清空购物车
        this.save(orders);
        orderDetailService.saveBatch(list);
        shoppingCartService.update().eq("user_id", userId).remove();

    }

    @Override
    public Page<Orders> userOrderPage(Integer page, Integer pageSize) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        Long userId = BaseContext.getCurrentId();
        return ordersMapper.userOrderPage(pageInfo, userId);
    }


}
